#!/usr/bin/env bash

# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
#
# SPDX-License-Identifier: Apache-2.0

set -e

export MAIN_REPO_DIR="$(readlink -f "${MAIN_REPO_DIR:-$(dirname ${0})/..}")"
export BINARY_PATH="$(readlink -f "${BINARY_PATH:-$(dirname ${0})/../bin}")"

pushd "${MAIN_REPO_DIR}" > /dev/null

echo "Fetching LD flags for build..."
ld_flags="$(hack/get-build-ld-flags.sh)"

for os in linux darwin windows; do
  for arch in amd64 arm64; do
    out_file="${BINARY_PATH}/gardenadm-${os}-${arch}"
    compress_command=(tar cvzf "${out_file}.tar.gz" -C $(dirname "${out_file}") $(basename "${out_file}"))
    if [[ "${os}" == "windows" ]]; then
      compress_command=(zip -j "${out_file}.zip" "${out_file}.exe")
      out_file="${out_file}.exe"
    fi

    echo "Building gardenadm for ${os}-${arch} and writing output to ${out_file}..."
    GOOS="${os}" GOARCH="${arch}" LD_FLAGS="${ld_flags}" BUILD_OUTPUT_FILE="${out_file}" BUILD_PACKAGES="./cmd/gardenadm" make build

    echo "Compressing ${out_file} to \"${compress_command[2]}\"..."
    "${compress_command[@]}"

    echo "Removing ${out_file}..."
    rm "${out_file}"
  done
done

popd > /dev/null
